The Euclidean Algorithm
Introduction
- The Euclidean Algorithm is one of the oldest algorithms in mathematics.
- It provides a fast and reliable way to compute the greatest common divisor (GCD) of two integers.
- Despite its ancient origins, it remains essential in modern mathematics and computer science.
What Is the Greatest Common Divisor?
- The greatest common divisor of two integers $a$ and $b$ is:
- the largest positive integer that divides both $a$ and $b$ without leaving a remainder.
- Examples:
- $\gcd(12, 18) = 6$
- $\gcd(20, 35) = 5$
- $\gcd(7, 13) = 1$ (these are coprime)
- Key facts:
- The GCD is always positive.
- If one number divides the other, the GCD is the smaller number.
- If two numbers share no common factors except $1$, their GCD is $1$.
Why Finding the GCD Matters
- The GCD appears in many areas:
- Reducing fractions
- Solving equations like $ax + by = c$
- Cryptography, especially algorithms like RSA
- Number theory, including modular arithmetic
- Computer science, where efficient algorithms matter
- The Euclidean Algorithm is far faster than listing all divisors.
A First Look at the Euclidean Algorithm
- The algorithm is based on a simple idea:
- Repeatedly replace a pair of numbers with a smaller pair that has the same GCD.
- Basic step:
- If $a$ and $b$ are integers with $a > b$, divide $a$ by $b$: $$a = bq + r.$$
- Then replace $(a, b)$ with $(b, r)$.
- Continue until the remainder becomes $0$.
- The last nonzero remainder is the GCD.
The Key Idea: Replacing Big Problems with Smaller Ones
- The crucial fact: $$\gcd(a, b) = \gcd(b, r).$$
- Why this helps:
- $r$ is always smaller than $b$.
- Each step shrinks the numbers.
- The process must eventually reach $0$.
- This shrinking is what makes the algorithm fast.
Step‑by‑Step Examples
Example 1: $\gcd(252, 105)$
- $252 = 105\cdot 2 + 42$
- $105 = 42\cdot 2 + 21$
- $42 = 21\cdot 2 + 0$
- Last nonzero remainder: 21
- So $\gcd(252, 105) = 21$.
Example 2: $\gcd(119, 34)$
- $119 = 34\cdot 3 + 17$
- $34 = 17\cdot 2 + 0$
- Last nonzero remainder: 17
- So $\gcd(119, 34) = 17$.
Example 3: $\gcd(48, 18)$
- $48 = 18\cdot 2 + 12$
- $18 = 12\cdot 1 + 6$
- $12 = 6\cdot 2 + 0$
- Last nonzero remainder: 6
- So $\gcd(48, 18) = 6$.
Why the Euclidean Algorithm Always Works
- The algorithm works because:
- Each step reduces the size of the numbers.
- The GCD does not change when replacing $(a, b)$ with $(b, r)$.
- Eventually the remainder becomes $0$.
- The last nonzero remainder must divide all previous remainders.
- Therefore, it is the greatest common divisor.
Extending the Algorithm to Negative Integers
- The GCD is always defined to be positive, even if inputs are negative.
- We can simply take absolute values: $$\gcd(a, b) = \gcd(|a|, |b|).$$
- The Euclidean Algorithm works the same way:
- Replace $a$ with $|a|$ and $b$ with $|b|$.
- Run the algorithm normally.
Common Mistakes and How to Avoid Them
- Mistake: Stopping too early.
- The algorithm ends only when the remainder is 0.
- Mistake: Forgetting to carry the remainder forward.
- Always replace $(a, b)$ with $(b, r)$.
- Mistake: Thinking the quotient matters.
- Only the remainder is used in the next step.
- Mistake: Mixing up the order.
- Always divide the larger number by the smaller one.
Calculator
GCD
- Returns the greatest common divisor of two numbers.
gcd(252, 105) gcd(48, 18)
GCD Verbose
- Calculates the greatest common divisor of two numbers.
- Shows the intermediate steps.
gcdVerbose(252, 105) gcdVerbose(48, 18)
Exercises
Try to compute each GCD using the Euclidean Algorithm.
Show the sequence of divisions when possible.
- Compute $\gcd(84, 30)$.
- Compute $\gcd(99, 78)$.
- Compute $\gcd(56, 15)$.
- Compute $\gcd(144, 60)$.
- Compute $\gcd(101, 23)$.
- Compute $\gcd(270, 192)$.
- Compute $\gcd(1{,}001, 143)$.
- Compute $\gcd(48, 7)$.
- Compute $\gcd(-72, 30)$ (remember to use absolute values).
- True or false: If $a$ divides $b$, then $\gcd(a, b) = a$.